package com.ciaojian.bytedance.controller;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.generator.config.IFileCreate;
import com.ciaojian.bytedance.service.ByteDanceNotifyService;
import com.ciaojian.bytedance.vo.bytedance.CallBackResponse;
import com.ciaojian.bytedance.vo.bytedance.ReceiveOrderQuery;
import com.ciaojian.core.bytedance.ByteDanceMiniConfig;
import com.ciaojian.core.bytedance.ByteDancePayConfig;
import com.ciaojian.core.bytedance.ByteDanceSignUtil;
import com.ciaojian.core.enums.business.CouponCodeOriginEnum;
import com.ciaojian.core.enums.business.CouponCodeTakeRecordTypeEnum;
import com.ciaojian.core.enums.business.OrderOrderTypeEnum;
import com.ciaojian.core.mapper.CouponCodeMapper;
import com.ciaojian.core.mapper.DyOrderMapper;
import com.ciaojian.core.mapper.OrderMapper;
import com.ciaojian.core.mapper.UserMapper;
import com.ciaojian.core.model.CouponCode;
import com.ciaojian.core.model.CouponCodeTakeRecord;
import com.ciaojian.core.model.Order;
import com.ciaojian.core.model.User;
import com.ciaojian.core.model.dy.DyOrder;
import com.ciaojian.core.model.vo.ByteDanceDeliveryVo;
import com.ciaojian.core.service.CouponService;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Atlakyn
 */
@Api(tags = "回调 Controller", hidden = true)
@RestController
@Slf4j
@RequestMapping("/notify")
public class NotifyController {

    @Resource
    private ByteDanceNotifyService byteDanceNotifyService;

    /**
     * 抖音开放平台 - 能力 - 支付 - 支付设置 -url
     * @param receiveOrderQuery
     * @return
     */
    @ApiOperation(value = "支付异步通知", notes = "支付异步通知")
    @PostMapping("/receiveOrder")
    @Transactional
    public Object receiveTicket(@RequestBody(required = false) ReceiveOrderQuery receiveOrderQuery) {
        return byteDanceNotifyService.receiveTicket(receiveOrderQuery);
    }

    /**
     * 担保支付回调
     * 抖音开放平台 - 能力 - 支付 - 支付设置 -url
     * @param receiveOrderQuery
     * @return
     */
    @ApiOperation(value = "支付剪发异步通知", notes = "支付剪发异步通知")
    @PostMapping("/jianfa/dyNotify")
    @Transactional
    public Object jianfaWxNotify(@RequestBody(required = false) ReceiveOrderQuery receiveOrderQuery) {
        return byteDanceNotifyService.jianfaWxNotify(receiveOrderQuery);
    }


    //移动网页 曾颖
    @GetMapping("/pushDelivery")
    public JSONObject pushDelivery(Integer orderId) {
        return byteDanceNotifyService.pushDelivery(orderId);

    }

    //小程序
    @GetMapping("/pushDeliveryMiniApp")
    public JSONObject pushDeliveryMiniApp(Integer orderId) {
        return byteDanceNotifyService.pushDeliveryMiniApp(orderId);

    }


}
